package net.example.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import net.example.manager.ClientGlobalCache;
import net.example.manager.Manager;
import net.example.model.BasicResult;
import net.example.model.ChangeMyPhoneResult;
import net.example.model.GGGroup;
import net.example.model.GGUser;
import net.example.model.UpdatePropertyType;
import net.example.ggtalk.R;
import net.example.utils.ToastUtils;

public class UpdatePropertyActivity extends Activity implements View.OnClickListener {

    private TextView title;
    private UpdatePropertyType updatePropertyType;
    private String json;
    private EditText edit_property;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_property);
        ((View)findViewById(R.id.ct_photo)).setVisibility(View.GONE);
        title=(TextView)findViewById(R.id.headerText);
        edit_property=(EditText) findViewById(R.id.edit_property);
        button=(Button) findViewById(R.id.btn_confirm);
        button.setText("确认");
        button.setVisibility(View.VISIBLE);
        button.setOnClickListener(this);
        json=getIntent().getStringExtra(getResources().getString(R.string.property_entity));
        String typeName =getIntent().getStringExtra(getResources().getString(R.string.update_property_type));
        updatePropertyType=  UpdatePropertyType.valueOf(UpdatePropertyType.class,typeName);
        this.initView();
    }

    private void initView()
    {
        String content="";
        switch (updatePropertyType)
        {
            case GroupName:
                this.title.setText("更改群名称");
                this.setEtCoustomLength(20);
                GGGroup group= JSON.parseObject(json, GGGroup.class);
                content =group.getName();
                break;
            case GroupAnnounce:
                this.title.setText("更改群公告");
                this.setEtCoustomLength(200);
                GGGroup group2= JSON.parseObject(json, GGGroup.class);
                content =group2.getAnnounce();
                break;
            case UserName:
                this.title.setText("更改名称");
                this.setEtCoustomLength(10);
                content =ClientGlobalCache.getInstance().getCurrentUser().getName();
                break;
            case UserSignature:
                this.title.setText("更改签名");
                this.setEtCoustomLength(50);
                content =ClientGlobalCache.getInstance().getCurrentUser().getSignature();
                break;
            case AddCatalog:
                this.title.setText("新增分组");
                this.setEtCoustomLength(20);
                break;
            case FriendCommentName:
                this.title.setText("修改备注");
                this.setEtCoustomLength(10);
                GGUser friend = ClientGlobalCache.getInstance().getUser(json);
                content = friend.getCommentName();
                break;
            case Phone:
                this.title.setText("更换手机");
                this.setEtCoustomLength(15);
                GGUser currentUser= ClientGlobalCache.getInstance().getCurrentUser();
                content = currentUser.Phone==null?"":currentUser.Phone;
                this.edit_property.setInputType(InputType.TYPE_CLASS_PHONE);
                break;
        }
        this.edit_property.setText(content);
        if(this.currentMaxCharLength>content.length())
        {
            this.edit_property.setSelection(content.length());//设置光标在最后一位
        }
        this.edit_property.requestFocus();//聚焦
    }

    private int currentMaxCharLength=0;//允许输入的最大字符数
    /**
     * 设置EditText的最大输入字符数
     * */
    private void setEtCoustomLength(int length){
        if (length>0) {
            this.currentMaxCharLength=length;
            edit_property.setFilters(new InputFilter[]{new InputFilter.LengthFilter(length)});
        }
    }

    @Override
    public void onClick(View view) {
        button.setEnabled(false);
        switch (updatePropertyType)
        {
            case GroupName:
                if(this.edit_property.getText().length()==0)
                {
                    ToastUtils.showShort(this, "群名称不能为空！");
                    return;
                }
                this.updateGroupProperty();
                break;
            case GroupAnnounce:
                this.updateGroupProperty();
                break;
            case UserName:
                if(this.edit_property.getText().length()==0)
                {
                    ToastUtils.showShort(this, "用户名不能为空！");
                    return;
                }
                this.updateUserProperty();
                break;
            case UserSignature:
                this.updateUserProperty();
                break;
            case AddCatalog:
                if(this.edit_property.getText().length()==0)
                {
                    return;
                }
                this.addFriendCatalog();
                break;
            case FriendCommentName:
//                if(this.edit_property.getText().length()==0)
//                {
//                    ToastUtils.showShort(this, "备注不能为空！");
//                    return;
//                }
                this.updateFriendCommentName();
                break;
            case Phone:
                this.updateUserPhone();
                break;
        }
        button.setEnabled(true);
    }

    private void updateUserPhone() {
        ChangeMyPhoneResult result = Manager.getInstance().changeMyPhone(this.edit_property.getText().toString().trim());
        String tips="";
        switch (result)
        {
            case UserNotExist:
            case Error:
                tips="内部错误！";
                break;
            case PhoneExisted:
                tips="电话号码已存在！";
                break;
            case Succeed:
                tips="保存成功！";
                break;
        }
        ToastUtils.showLong(this, tips);
    }

    private void updateGroupProperty()
    {
        GGGroup group= JSON.parseObject(json, GGGroup.class);
        BasicResult result=BasicResult.Succeed;
        if(updatePropertyType==UpdatePropertyType.GroupName)
        {
            result = Manager.getInstance().changeGroupInfo(group.getGroupID(),this.edit_property.getText().toString(),group.getAnnounce());
        }
        else if(updatePropertyType==UpdatePropertyType.GroupAnnounce)
        {
            result=  Manager.getInstance().changeGroupInfo(group.getGroupID(),group.getName(),this.edit_property.getText().toString());
        }
        this.showResult(result);
    }

    private void updateUserProperty()
    {
        GGUser currentUser= ClientGlobalCache.getInstance().getCurrentUser();
        BasicResult result=BasicResult.Succeed;
        if(updatePropertyType==UpdatePropertyType.UserName)
        {
            result = Manager.getInstance().changeMyBaseInfo(this.edit_property.getText().toString(),currentUser.getSignature(),currentUser.getOrgID());
        }
        else if(updatePropertyType==UpdatePropertyType.UserSignature)
        {
            result=  Manager.getInstance().changeMyBaseInfo(currentUser.getName(),this.edit_property.getText().toString(),currentUser.getOrgID());
        }
        this.showResult(result);
    }

    private void addFriendCatalog()
    {
        if(this.edit_property.getText().toString().equals(getResources().getString(R.string.blacklist)))
        {
            ToastUtils.showLong(this, "非法名称");
            return;
        }
        BasicResult result= Manager.getInstance().addFriendCatalog(this.edit_property.getText().toString());
        this.showResult(result);
    }

    private void updateFriendCommentName()
    {
       String commentName = this.edit_property.getText().toString();
        GGUser friend = ClientGlobalCache.getInstance().getUser(json);
       BasicResult result =Manager.getInstance().updateFriendCommentName(friend.getUserID(),commentName);
       this.showResult(result);
    }

    private void showResult(BasicResult result)
    {
        if(result==BasicResult.Succeed)
        {
            ToastUtils.showLong(this, "保存成功！");
            finish();
        }else {
            ToastUtils.showLong(this, "内部错误！");
        }
    }
    public void finish(View view)
    {
        finish();
    }
}
